﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuildSDFDatabase
{
	class Program
	{
		static void Main(string[] args)
		{
			var databaseDir = args[0];
			ResearchDBCreate(databaseDir);

			using (DatabaseAccessor dbAccessor = new DatabaseAccessor(Path.Combine(databaseDir, "research.sdf")))
			using (IntermediateDataBuilder idBuilder = new IntermediateDataBuilder(dbAccessor))
			{
				idBuilder.BuildDisplayTree();
				idBuilder.BuildKeywordTFIDF();
			}
		}

		private static void ResearchDBCreate(string dbDir)
		{
			using (DatabaseAccessor dbAccessor = new DatabaseAccessor(Path.Combine(dbDir, "research.sdf")))
			{
				for (int i = 0; i <= 25; i++)
					dbAccessor.DBExecuteNonQuery(TablesCreater.TablesCreate(i));

				ResearchDataParser dataParser = new ResearchDataParser(dbDir);
				List<string> times = new List<string>()
				{
					dataParser.startTime.ToString(),
					dataParser.endTime.ToString(),
				};
				dbAccessor.InsertOneItem("DataTime", times);
				dbAccessor.BulkInsert(dataParser.GetCoporaTables());

				Console.WriteLine("\r\nArchive field: infovis");
				dataParser.LoadClusterMeta(FieldName.Infovis);
				dbAccessor.BulkInsert(dataParser.GetClusterTables());
				dataParser.LoadDocument(FieldName.Infovis);
				dbAccessor.BulkInsert(dataParser.GetDocumentTables());
				dataParser.LoadLeadLagMeta(FieldName.Infovis);
				dbAccessor.BulkInsert(dataParser.GetLeadLagTables());

				Console.WriteLine("\r\nArchive field: mining");
				dataParser.LoadClusterMeta(FieldName.Mining);
				dbAccessor.BulkInsert(dataParser.GetClusterTables());
				dataParser.LoadDocument(FieldName.Mining);
				dbAccessor.BulkInsert(dataParser.GetDocumentTables());
				dataParser.LoadLeadLagMeta(FieldName.Mining);
				dbAccessor.BulkInsert(dataParser.GetLeadLagTables());

				Console.WriteLine("\r\nArchive field: graphics");
				dataParser.LoadClusterMeta(FieldName.Graphics);
				dbAccessor.BulkInsert(dataParser.GetClusterTables());
				dataParser.LoadDocument(FieldName.Graphics);
				dbAccessor.BulkInsert(dataParser.GetDocumentTables());
				dataParser.LoadLeadLagMeta(FieldName.Graphics);
				dbAccessor.BulkInsert(dataParser.GetLeadLagTables());
			}
		}
	}
}

//FieldDataParser infovisDataParser = new FieldDataParser("infovis",
//									@"D:\Topic Comparison\Data mining\Data\researchData");
//infovisDataParser.LoadLeadLagMeta();
//dbBuilder.BulkInsert(infovisDataParser.RoseTreeDT);
//dbBuilder.BulkInsert(infovisDataParser.GlobalLeadLagDT);
//dbBuilder.BulkInsert(infovisDataParser.LocalLeadLagDT);
//dbBuilder.BulkInsert(infovisDataParser.DocumentMatrixDT);
//infovisDataParser.ArchiveDocument();
//dbBuilder.BulkInsert(infovisDataParser.DocumentDT);
//dbBuilder.BulkInsert(infovisDataParser.ClusterDocumentDT);
//dbBuilder.BulkInsert(infovisDataParser.DocumentPropertiesDT);
//infovisDataParser.ArchiveClusterMeta();
//dbBuilder.BulkInsert(infovisDataParser.ClusterInfoDT);
//dbBuilder.BulkInsert(infovisDataParser.ClusterCorpusDT);
//dbBuilder.BulkInsert(infovisDataParser.ClusterCorpusKeywordDT);

//DatabaseQueryer dbQueryer = new DatabaseQueryer(dbBuilder);
//int size = dbQueryer.GetClusterCorpusSize(Field.Infovis, 0, new DateTime(2000, 1, 1), Corpus.Proposal);
//int size1 = dbQueryer.GetClusterCorpusSize(Field.Infovis, 0, new DateTime(2000, 1, 1), Corpus.Paper);

//var words = dbQueryer.GetClusterCorpusKeywordInfo(Field.Infovis, 0, new DateTime(2000, 1, 1), Corpus.Proposal);
//var words1 = dbQueryer.GetClusterCorpusKeywordInfo(Field.Infovis, 0, new DateTime(2000, 1, 1), Corpus.Paper);

//var authors = dbQueryer.GetDocumentProperty(842, PaperProperty.Authors);
//var pi = dbQueryer.GetDocumentProperty(0083421, ProposalProperty.PrincipalInvestigator);

//var docs = dbQueryer.GetDocumentIdByClusterCorpus(Field.Infovis, 7, Corpus.Proposal);
//var docs1 = dbQueryer.GetDocumentIdByClusterCorpus(Field.Infovis, 7, Corpus.Paper);

//var timestamp = dbQueryer.GetDocumentAttribute(9988357, DocumentAttribute.Timestamp);
//var title = dbQueryer.GetDocumentAttribute(9988357, DocumentAttribute.Title);
//var abstr = dbQueryer.GetDocumentAttribute(9988357, DocumentAttribute.Abstract);
//var corpus = dbQueryer.GetDocumentAttribute(9988357, DocumentAttribute.Corpus);

//var tree = dbQueryer.GetFieldRoseTree(Field.Infovis);

//var gleadlagValue = dbQueryer.GetGlobalLeadLagValue(Field.Infovis, "cluster7", Corpus.Proposal, Corpus.Paper);

//var lleadlagValue = dbQueryer.GetLocalLeadLagValues(Field.Infovis, 7, Corpus.Proposal, Corpus.Paper);

//var docMatrix = dbQueryer.GetClusterDocumentMatrix(Field.Infovis, 7);